﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.Lib;
using QuanLyBanHang.Report;

namespace QuanLyBanHang.GUI
{
    public partial class frmKhachHang : Form
    {
        public frmKhachHang()
        {
            InitializeComponent();
        }
        DataTable dtpKhachHang  = KetNoi.GetTable("select * from tblKhachHang"); 
        
        BindingSource bs = new BindingSource();
        public int i = 0;

        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            LoaddtgKhachHang();
            
           
        }

        public void LoaddtgKhachHang()
        {
            bs.DataSource = KetNoi.GetTable("select * from tblKhachHang"); 
            dtgKhachHang.DataSource = bs;
            dtgKhachHang.AllowUserToAddRows = false;
            dtgKhachHang.AllowUserToDeleteRows = false;
            dtgKhachHang.Columns["MaKH"].HeaderText = "Mã Khách Hàng";
            dtgKhachHang.Columns["MaKH"].Width = 120;
            dtgKhachHang.Columns["TenKH"].HeaderText="Tên Khách Hàng";
            dtgKhachHang.Columns["TenKH"].Width = 150;
            dtgKhachHang.Columns["DiaChi"].HeaderText = "Địa chỉ";
            dtgKhachHang.Columns["DiaChi"].Width = 150;
            dtgKhachHang.Columns["SoDT"].HeaderText = "Số Điện Thoại";
            dtgKhachHang.Columns["SoTK"].HeaderText = "Số tài khoản";
            Lock();
        }

        private void dtgKhachHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaKhachHang.Enabled = false;
            if (e.RowIndex >= 0)
            {
                txtMaKhachHang.Text = dtpKhachHang.Rows[e.RowIndex]["MaKH"].ToString();
                txtHoTen.Text = dtpKhachHang.Rows[e.RowIndex]["TenKH"].ToString();
                txtSoDienThoai.Text = dtpKhachHang.Rows[e.RowIndex]["DiaChi"].ToString();
                txtDiaChi.Text = dtpKhachHang.Rows[e.RowIndex]["SoDT"].ToString();
                txtSoTaiKhoan.Text = dtpKhachHang.Rows[e.RowIndex]["SoTK"].ToString();
            }
        }
        public void Lock()
        {
            txtMaKhachHang.Enabled = false;
            txtHoTen.Enabled = false;
            txtSoDienThoai.Enabled = false;
            txtDiaChi.Enabled = false;
            txtSoTaiKhoan.Enabled = false;
            btnIn.Enabled = true;
            btnIn.Enabled = true;
            btnXoa.Enabled = true;
            btnThoat.Enabled = true;
            btnSua.Enabled = true;
            btnThoat.Enabled = true;
        }

        public void unlock()
        {
            txtMaKhachHang.Enabled = true;
            txtHoTen.Enabled = true;
            txtSoDienThoai.Enabled = true;
            txtDiaChi.Enabled = true;
            txtSoTaiKhoan.Enabled = true;
            btnIn.Enabled = false;
            btnIn.Enabled = true;
            btnXoa.Enabled = false;
            btnThoat.Enabled = true;
            btnSua.Enabled = false;
            btnThoat.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtMaKhachHang.Enabled == true)
            {
                DataRow newr = dtpKhachHang.NewRow();
                newr["MaKH"] = txtMaKhachHang.Text;
                newr["TenKH"] = txtHoTen.Text;
                newr["DiaChi"] = txtDiaChi.Text;
                newr["SoDT"] = txtSoDienThoai.Text;
                newr["SoTK"] = txtSoTaiKhoan.Text;
                i = KetNoi.ExcuteSQL("insert into tblKhachHang(MaKH,TenKH,DiaChi,SoDT,SoTK) values('" + newr["MaKH"] + "',N'"
                                                                                                         + newr["TenKH"] + "',N'"
                                                                                                         + newr["DiaChi"] + "',N'"
                                                                                                         + newr["SoDT"] + "',N'"
                                                                                                         + newr["SoTK"] + "')");
                if (i > 0)
                {
                    MessageBox.Show("Lưu thành công");
                }
                else
                {
                    MessageBox.Show("Lưu thất bại");
                }
                LoaddtgKhachHang();
            }
            else
            {
                DataRow newr = dtpKhachHang.NewRow();
                newr["MaKH"] = txtMaKhachHang.Text;
                newr["TenKH"] = txtHoTen.Text;
                newr["DiaChi"] = txtSoDienThoai.Text;
                newr["SoDT"] = txtDiaChi.Text;
                newr["SoTK"] = txtSoTaiKhoan.Text;
                i = KetNoi.ExcuteSQL("update tblKhachHang set TenKH=N'" + newr["TenKH"]
                                                           + "',DiaChi=N'" + newr["DiaChi"] 
                                                           + "',SoDT=N'" + newr["SoDT"]
                                                           + "',SoTK=N'" + newr["SoTK"] 
                                                           + "'where MaKH='" + newr["MaKH"] + "'");
                if (i > 0)
                {
                    MessageBox.Show("Lưu thành công");
                }
                else
                {
                    MessageBox.Show("Lưu thất bại");
                }
                LoaddtgKhachHang();
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtMaKhachHang.Text = "";
            txtHoTen.Text = "";
            txtDiaChi.Text = "";
            txtSoDienThoai.Text = "";
            txtSoTaiKhoan.Text = "";
            unlock();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            unlock();
            txtMaKhachHang.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult dialogrs = MessageBox.Show("Bạn chắc chắn muốn xóa ?","Cảnh báo",MessageBoxButtons.YesNo);

            if(dialogrs == DialogResult.Yes)
            {
                if (txtMaKhachHang.Text == "")
                {
                    MessageBox.Show("Chưa chọn mã khách hàng");
                }
                else
                {
                    int i = KetNoi.ExcuteSQL("delete from tblKhachHang where MaKH = '"+txtMaKhachHang.Text+"'");
                    if (i > 0)
                    {
                        MessageBox.Show("Xóa thành công");
                        LoaddtgKhachHang();
                    }
                }
            }

        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            //dtgKhachHang.DataSource = KetNoi.GetTable("select * from tblKhachHang where MaKH like'" + '%' + txtTimKiem.Text + "%" + "' or TenKH like'" + '%' + txtTimKiem.Text + '%' + "'");
            dtpKhachHang = KetNoi.GetTable("select * from tblKhachHang where MaKH like'" + '%' + txtTimKiem.Text + "%" + "' or TenKH like'" + '%' + txtTimKiem.Text + '%' + "'");
            bs.Filter = string.Format("MaKH like '%{0}%'",txtTimKiem.Text);
            dtgKhachHang.AllowUserToAddRows = false;
            dtgKhachHang.AllowUserToDeleteRows = false;
            dtgKhachHang.Columns["MaKH"].HeaderText = "Mã Khách Hàng";
            dtgKhachHang.Columns["MaKH"].Width = 100;
            dtgKhachHang.Columns["TenKH"].HeaderText = "Tên Khách Hàng";
            dtgKhachHang.Columns["TenKH"].Width = 250;
            dtgKhachHang.Columns["SoTK"].HeaderText = "Số tài khoản";
            dtgKhachHang.Columns["SoDT"].HeaderText = "Số Điện Thoại";
            Lock();
        }

        private void dtgKhachHang_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            for (byte j = 0; j <dtgKhachHang.Rows.Count; j++)
            {
                dtgKhachHang.Rows[j].DefaultCellStyle.BackColor = Color.Honeydew;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            if (dtpKhachHang == null)
                dtpKhachHang = KetNoi.GetTable("select * from tblKhachHang");
            crsKhachHang _crkh = new crsKhachHang(dtpKhachHang);
            _crkh.Show();
        }
    }
}
